public class SelectionSort {
    public static void main(String[] args) {
        //
        Integer[] array = {9, 6, 3, 2, 5, 5, 1, 7, 8, 2, 4, 3};
        System.out.println("\nselectionSortRes:");
        Integer[] selectionSortRes = selectionSort(array);
        for (int i = 0; i < selectionSortRes.length; i++) {
            System.out.printf(selectionSortRes[i] + ",");
        }
    }

    // selectionSort-01
    public static Integer[] selectionSort(Integer[] array) {
        for (int i = 0; i < array.length; i++) {
            int minx = i;
//            array[minx] = array[i];
            for (int j = i + 1; j < array.length; j++) {
                if (array[i] > array[j]) {
                    // 保存最小数据的索引
                    minx = j;
                }
            }
            if (minx != i) {
                // 此种交换，算是全局选出一个最小值，然后做交换，这种交换是不稳定的
                int temp = array[i];
                array[i] = array[minx];
                array[minx] = temp;
            }
        }
        return array;
    }
}
